package net.lgpage.util;

import net.lgpage.http.PersistentCookieStore;

import org.apache.http.client.CookieStore;

import android.content.Context;
import android.os.Bundle;

import com.matriq.cloudmanager.MyApp;
import com.matriq.cloudmanager.entity.User;

public class Saver {

	public static void saveCookies(Context context) {
		PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
		CookieStore cs = MyApp.getApp().getCookieStore();
		if (cs != null) {
			for (int i = 0; i < cs.getCookies().size(); i++) {
				myCookieStore.addCookie(cs.getCookies().get(i));
			}
		}
	}

	public static void loadCookies(Context context) {
		PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
		MyApp.getApp().setCookieStore(myCookieStore);
	}
	
	public static void clearCookies(Context context){
		PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
		myCookieStore.clear();
		MyApp.getApp().setCookieStore(null);
	}
	
	public static void saveMyApp(Bundle outState){
		if(MyApp.getApp().getLoginObj()!=null)
		outState.putSerializable("loginObj", MyApp.getApp().getLoginObj());
		if(MyApp.getApp().getUsername()!=null){
			outState.putString("username", MyApp.getApp().getUsername());
		}
	}
	
	public static void loadMyApp(Bundle savedInstanceState){
		if(savedInstanceState == null)return;
		BaseUser loginObj =  (BaseUser) savedInstanceState.getSerializable("loginObj");
		if(loginObj != null)
		MyApp.getApp().setLoginObj((User) loginObj);
		String username = savedInstanceState.getString("username");
		if(username!=null){
			MyApp.getApp().setUsername(username);
		}
	}
	
}
